/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ca.barrenechea.stickyheaders.adapter;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;

import java.util.List;

import ca.barrenechea.widget.recyclerview.decoration.holder.HeaderHolder;
import ca.barrenechea.widget.recyclerview.decoration.holder.InlineHeaderHolder;
import ca.barrenechea.widget.recyclerview.decoration.holder.ItemHolder;
import ca.barrenechea.widget.recyclerview.decoration.model.DoubleHeaderModel;
import ca.barrenechea.widget.recyclerview.decoration.model.InlineHeaderModel;
import ca.barrenechea.widget.recyclerview.decoration.model.ItemModel;
import ca.barrenechea.widget.recyclerview.decoration.model.Model;

import static ca.barrenechea.stickyheaders.page.BasePage.MARGIN_TOP;

/**
 * 单粘性+单内嵌Item适配器
 *
 * @since 2021-04-16
 */
public class InlineDoubleHeaderTestAdapter extends BaseItemProvider {
    private Context context;
    private List<ItemModel> data;

    /**
     * InlineDouble滑动列表构造函数
     *
     * @param context 上下文
     * @param dataSource 数据源
     */
    public InlineDoubleHeaderTestAdapter(Context context, List<ItemModel> dataSource) {
        this.context = context;
        this.data = dataSource;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null ? data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Model model = data.get(position);
        HeaderHolder headerHolder = null;
        InlineHeaderHolder doubleHolder = null;
        ItemHolder itemHolder = null;
        Component itemView = null;
        if (model.isHeader()) {
            if (model.getType() == DoubleHeaderModel.TYPE) {
                itemView = LayoutScatter.getInstance(context).parse(model.getResource(position), null, false);
                headerHolder = new HeaderHolder(context, itemView);
            } else if (model.getType() == InlineHeaderModel.TYPE) {
                itemView = LayoutScatter.getInstance(context).parse(model.getResource(position), null, false);
                doubleHolder = new InlineHeaderHolder(context, itemView);
            }
        } else {
            itemView = LayoutScatter.getInstance(context).parse(model.getResource(position), null, false);
            itemHolder = new ItemHolder(context, itemView);
        }
        if (headerHolder != null) {
            headerHolder.headerText.setText(model.getTitle());
        }
        if (doubleHolder != null) {
            doubleHolder.headerText.setText(model.getTitle());
            doubleHolder.contentText.setText(model.getMessage());
        }

        if (itemHolder != null) {
            itemHolder.itemText.setText(model.getTitle());
        }
        if (position == 0 && itemView != null) {
            itemView.setMarginTop(MARGIN_TOP);
        }

        return itemView;
    }
}
